package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.Select.UserSelect;
import com.sand.dto.merchant.merchantDto;
import com.sand.dto.supplier.*;
import com.sand.entity.sell.Purchase;
import com.sand.entity.sell.PurchaseDetail;
import com.sand.feign.UserFeign;
import com.sand.feign.merchantFeign;
import com.sand.feign.supplierFeign;
import com.sand.mapper.PurchaseMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.service.LogisticsService;
import com.sand.service.PurchaseDetailService;
import com.sand.service.PurchaseService;
import com.sand.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.util.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private merchantFeign merchantFeign;

    @Autowired
    private supplierFeign supplierFeign;
    /**
     * 发起采购
     *
     * @param purchaseAdd
     */
    @Override
    @Transactional
    public Integer add(PurchaseAdd purchaseAdd) {

        //参数校验
        pan(purchaseAdd);

        //计算商品的总金额
        Integer money = suan(purchaseAdd.getArr());

        //保存采购
        Purchase purchase = new Purchase();

        purchase.setMerchantid(purchaseAdd.getMerchantid());
        purchase.setSupplierid(purchaseAdd.getSupplierid());
        purchase.setSubscription(purchaseAdd.getSubscription());

        //判断是否付全款
        if (purchaseAdd.getSubscription() >= money){
            purchase.setWhether(1);
        }else {
            purchase.setWhether(0); //默认采购没有完成
        }

        purchase.setMoney(money);

        save(purchase);

        //保存采购的商品
        ArrayList<PurchaseDetail> list = new ArrayList<>();

        for (PurchaseAddShopping shopping : purchaseAdd.getArr()) {
            PurchaseDetail purchaseDetail = new PurchaseDetail();

            purchaseDetail.setShoppingid(shopping.getShoppingid());
            purchaseDetail.setAmount(shopping.getAmount());
            purchaseDetail.setPrice(shopping.getPrice());
            purchaseDetail.setPurchaseid(purchase.getId());

            list.add(purchaseDetail);
        }

        purchaseDetailService.saveBatch(list);

        return 1;
    }

    /**
     * 条件查询采购单
     *
     * @param selectePurchase
     */
    @Override
    public List<Purchase> get(SelectePurchase selectePurchase) {

        Integer purchaseid = selectePurchase.getPurchaseid();
        Integer whether = selectePurchase.getWhether();
        Integer merchantid = selectePurchase.getMerchantid();
        Integer supplierid = selectePurchase.getSupplierid();

        LambdaQueryWrapper<Purchase> wrapper = new LambdaQueryWrapper<>();

        if (purchaseid != null){
            wrapper.eq(Purchase::getId,purchaseid);
        }

        if (whether != null){
            wrapper.eq(Purchase::getWhether,whether);
        }

        if (merchantid != null){
            wrapper.eq(Purchase::getMerchantid,merchantid);
        }

        if (supplierid != null){
            wrapper.eq(Purchase::getSupplierid,supplierid);
        }

        return list(wrapper);
    }

    /**
     * 查看采购详细
     *
     * @param purchaseid
     */
    @Override
    public PurchaseAll all(Integer purchaseid) {

        if (purchaseid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入采购单id");
        }

        Purchase purchase = getById(purchaseid);

        if (purchase == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有这个采购单");
        }

        //获取这个采购单的采购详细
        List<PurchaseDetail> list = purchaseDetailService.list(new LambdaQueryWrapper<PurchaseDetail>().eq(PurchaseDetail::getPurchaseid, purchaseid));

        PurchaseAll purchaseAll = new PurchaseAll();

        purchaseAll.setMerchantid(purchase.getMerchantid());
        purchaseAll.setSupplierid(purchase.getSupplierid());
        purchaseAll.setArr(list.toArray(new PurchaseDetail[0]));

        return purchaseAll;
    }

    /**
     * 完成采购
     *
     * @param purchaseid
     */
    @Override
    public Integer ok(Integer purchaseid) {

        if (purchaseid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入采购单");
        }

        Purchase purchase = getById(purchaseid);

        if (purchase == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"采购单不存在");
        }

        if (purchase.getWhether() != 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"当前采购单已经完成");
        }

        //修改
        purchase.setWhether(1);

        updateById(purchase);

        return 1;
    }

    //采购的参数校验
    private void pan(PurchaseAdd purchaseAdd) {

        Integer supplierid = purchaseAdd.getSupplierid();

        Integer merchantid = purchaseAdd.getMerchantid();

        Integer subscription = purchaseAdd.getSubscription();

        PurchaseAddShopping[] arr = purchaseAdd.getArr();

        if (merchantid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入采购人");
        }

        if (supplierid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入供货商");
        }

        //判断采购人
        merchantDto merchantDto = merchantFeign.select(new UserSelect(merchantid, null)).getData();

        if (merchantDto == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"当前采购人id错误");
        }

        if (merchantDto.getPermission() == 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"当前采购人权限不够");
        }

        //判断供货商
        //SupplierDto supplierDto = supplierService.select(new UserSelect(supplierid, null));

        SupplierDto supplierDto = supplierFeign.select(new UserSelect(supplierid, null)).getData();

        if (supplierDto == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"当前供货商不存在");
        }


        if (subscription == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入订金");
        }

        if (arr == null || arr.length == 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入采购的商品");
        }




    }


    private Integer suan(PurchaseAddShopping[] arr){

        int count = 0;

        for (PurchaseAddShopping shopping : arr) {
            //数量
            Integer amount = shopping.getAmount();
            //价格
            Integer price = shopping.getPrice();
            //计算
            count += amount * price;
        }

        return count;
    }

}
